<?php
/**
 * 《统计分析》实现类
 *
 * @author 孙晓晔
 * @version $Id: Mold.class.php 1053 2010-04-07 11:15:15Z sunxy $
 */
require_once(dirname(__FILE__) . '/MaterialUtil.class.php');

class Tj extends MaterialUtil {

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}
	
	/**
	 * 
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function tab_in($controller, $request) {		
		$data = array(
			'物料需求' => array('cc'=>'material.php?do=Bom.group', 'js'=>''),
			'采购信息' => array('cc'=>'material.php?do=Purchase.group', 'js'=>''),
			'采购明细' => array('cc'=>'material.php?do=RecordIn.group', 'js'=>''),
			'入库明细' => array('cc'=>'material.php?do=In.group', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function tab_out($controller, $request) {		
		$data = array(
			'物料库存' => array('cc'=>'material.php?do=Amount.group', 'js'=>''),		
			'用料申请' => array('cc'=>'material.php?do=Used.group', 'js'=>''),
			'用料明细' => array('cc'=>'material.php?do=RecordOut.group', 'js'=>''),
			'出库明细' => array('cc'=>'material.php?do=Out.group', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function ajax_in($controller, $request) {
		$request['tbl'] = "sj.material_in";
		
		$this->ajax_($controller, $request);
	}
	
	function ajax_out($controller, $request) {
		$request['tbl'] = "sj.material_out";
		
		$this->ajax_($controller, $request);
	}
	
	function ajax_kc($controller, $request) {
		$request['tbl'] = "sj.material_amount";
		
		$this->ajax_($controller, $request);
	}
	
	function ajax_($controller, $request) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT name, standard, count(*) FROM %s GROUP BY name, standard";
		// $fmt = "SELECT name, standard, count(*) FROM %s GROUP BY name, standard limit 22";
		$sql = sprintf($fmt, $request['tbl']);
		// echo $sql;
		$result = $db->query($sql);
		
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$name = addslashes($row['name']);
			$standard = addslashes($row['standard']);
			
			$row['_in'] = $this->_item("sj.material_in", $name, $standard);
			$row['_out'] = $this->_item("sj.material_out", $name, $standard);
			$row['_kc'] = $this->_item("sj.material_amount", $name, $standard);
			
			$ret[] = $row;
		}
		
		// print_r($ret);
		
		$controller->assign_by_ref('result', $ret);
		
		$controller->display($request, 'material/a');
		// $a = $controller->sunxy($request, 'material/a');
		// echo $a;
	}
	
	function _item($tbl, $name, $standard) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE name = '%s' AND standard = '%s'";
		$sql = sprintf($fmt, $tbl, $name, $standard);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
		 	$row['emp_t'] = Name::emp($row['emp']);
		 	$ret[] = $row;
		}
		 
		return $ret;
	}
}
?>